package org.example.plugin;

import cn.hutool.core.date.DateTime;
import org.smartboot.socket.transport.AioSession;
import org.smartboot.socket.transport.WriteBuffer;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author Tamako
 * @since 2025-07-21 09:35
 */
public class HeartBeat extends HeartPlugin<String> {
    public static final String RESET = "\u001B[0m";
    public static final String RED = "\u001B[31m";
    public static final String GREEN = "\u001B[32m";
    public static final String YELLOW = "\u001B[33m";
    public static final String BLUE = "\u001B[34m";

    public HeartBeat() {
        super(5, 7, TimeUnit.SECONDS);
    }

    @Override
    public void sendHeartRequest(AioSession session) throws IOException {
        WriteBuffer writeBuffer = session.writeBuffer();
        DateTime now = DateTime.now();
        System.out.println(RED + now.toMsStr() + "发送心跳"+now.toTimestamp().getTime() + RESET);
        byte[] content = "heart beat".getBytes();
        writeBuffer.writeInt(content.length);
        writeBuffer.write(content);
    }

    @Override
    public boolean isHeartMessage(AioSession session, String msg) {
        boolean equals = "heart beat".equals(msg);
        if (equals) {
            DateTime now = DateTime.now();
            System.out.println(GREEN + now.toMsStr() + "接收心跳"+now.toTimestamp().getTime() + RESET);
        }
        return equals;
    }

}
